import os

import Logger

"""
This function handles a command from a client.
Parses it and performs the desired action.
"""
def clientCommand(cmd,optdb):
    try:
        lirc_cmd = optdb['lirc_cmds'][cmd]
        #irsend simulate "000000037ff07be1 00 Up mceusb"
        sys_cmd = "/usr/bin/irsend simulate \""+lirc_cmd[0]+" 00 "+lirc_cmd[1]+" "+optdb['remote']+"\""
        os.system(sys_cmd)
        Logger.printLog("Ran: "+ sys_cmd)
    except KeyError:
        #check if custom command
        try:
            custom_cmd =  optdb['custom_cmds'][cmd]
            os.system(custom_cmd)
            Logger.printLog("Ran: "+ custom_cmd)
        except KeyError:
            Logger.printLog("Command not recognised: "+cmd)
        except:
            Logger.printLog("Error Running custom cmd:"+custom_cmd)
    except:
        Logger.printLog("Error Running lric cmd:"+sys_cmd)

